# -*- coding:utf-8 -*-
import os
import sqlite3
from PyQt5.QtCore import Qt, pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import QMainWindow, QAction, QFileDialog, QMessageBox, QDockWidget, QHBoxLayout
from PyQt5.QtGui import QKeySequence
from tabwindow import TabWindow

class MainWindow(QMainWindow):
    databaseOpened = pyqtSignal(str)
    databaseExecute = pyqtSignal()
    databaseClose = pyqtSignal()
    databaseNewQuery = pyqtSignal()
    def __init__(self):
        super(MainWindow, self).__init__()

        self.openAction = QAction("打开", self)
        self.openAction.setShortcut('Ctrl+O')
        self.openAction.setStatusTip('打开数据库连接')
        self.openAction.triggered.connect(self.openDatabase)

        self.createAction = QAction("创建", self)
        self.createAction.setShortcut('Ctrl+T')
        self.createAction.setStatusTip("创建SQLite数据库")
        self.createAction.triggered.connect(self.createDatabase)

        self.closeAction = QAction("关闭", self)
        self.closeAction.setShortcut('Ctrl+Q')
        self.closeAction.setStatusTip("关闭数据库连接")
        self.closeAction.triggered.connect(self.closeDatabase)
        self.closeAction.setEnabled(False)

        self.queryAction = QAction("执行", self)
        self.queryAction.setShortcut(QKeySequence.Refresh)
        self.queryAction.setStatusTip("执行选中的SQL")
        self.queryAction.setEnabled(False)
        self.queryAction.triggered.connect(self.execute)

        self.newQueryAction = QAction("新建查询", self)
        self.newQueryAction.setShortcut('Ctrl+N')
        self.newQueryAction.setStatusTip("新建查询窗口")
        self.newQueryAction.setEnabled(False)
        self.newQueryAction.triggered.connect(self.newQuery)

        menu = self.menuBar()
        fileMenu = menu.addMenu("文件(&F)")
        fileMenu.addAction(self.openAction)
        fileMenu.addAction(self.createAction)
        fileMenu.addAction(self.closeAction)

        helpMenu = menu.addMenu('帮助(&H)')
        aboutAction = QAction('关于', self)
        aboutAction.triggered.connect(self.about)
        helpMenu.addAction(aboutAction)

        self.toolBar = self.addToolBar('工具栏')
        self.toolBar.addAction(self.openAction)
        self.toolBar.addAction(self.queryAction)
        self.toolBar.addAction(self.newQueryAction)

        self.tab = TabWindow()
        self.setCentralWidget(self.tab)
        self.databaseExecute.connect(self.tab.executeQuery)
        self.databaseClose.connect(self.tab.closeConnection)
        self.databaseOpened.connect(self.tab.setOpen)
        self.databaseNewQuery.connect(self.tab.openQuery)

        self.setWindowTitle("PySQLite")
        self.showMaximized()

    def openDatabase(self):
        fileName, _ = QFileDialog.getOpenFileName(self, "打开", "./", "SQLite数据库(*.db)")
        if fileName == '':
            return
        
        self.setStatus("成功打开数据库: %s" % fileName)
        self.databaseOpened.emit(fileName)
        self.queryAction.setEnabled(True)
        self.newQueryAction.setEnabled(True)
        self.closeAction.setEnabled(True)

    def closeDatabase(self):
        self.openAction.setEnabled(True)
        self.createAction.setEnabled(True)
        self.closeAction.setEnabled(False)

    def createDatabase(self):
        fileName, _ = QFileDialog.getSaveFileName(self, "创建", "./", "SQLite数据库(*.db)")
        if fileName == '':
            return

        self.setStatus("成功创建数据库: %s" % fileName)
        self.databaseOpened.emit(fileName)

    def execute(self):
        self.databaseExecute.emit()

    def newQuery(self):
        self.databaseNewQuery.emit()

    @pyqtSlot(str)
    def setStatus(self, msg):
        status = self.statusBar()
        status.showMessage(msg)

    def about(self):
        QMessageBox.information(self, "关于 PySQLite", "作者: 方俊")
